package com.itheima.api.sms;

import cn.hutool.db.PageResult;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.api.pms.PmsProductService;
import com.itheima.mapper.sms.SmsFlashPromotionProductRelationMapper;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.dto.SmsFlashPromotionProductRelationDto;
import com.itheima.vo.pojo.PmsProduct;
import com.itheima.vo.pojo.SmsFlashPromotionProductRelation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class SmsFlashPromotionProductRelationServiceImpl extends ServiceImpl<SmsFlashPromotionProductRelationMapper, SmsFlashPromotionProductRelation> implements SmsFlashPromotionProductRelationService{

    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;

    @Autowired
    private PmsProductService pmsProductService;

    //获取商品数量
    @Override
    public long getCount(Long flashPromotionId, Long flashPromotionSessionId) {

        return relationMapper.count(flashPromotionId, flashPromotionSessionId);
    }

    /*//分页查询相关商品及限时购促销信息
    @Override
    public TableDataInfo listAll(Integer pageNum, Integer pageSize, Long flashPromotionId, Long flashPromotionSessionId) {
        Page<SmsFlashPromotionProductRelation> pageInfo = new Page<>(pageNum,pageSize);
        TableDataInfo tableDataInfo = new TableDataInfo();
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(flashPromotionId != null, SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        lqw.eq(flashPromotionSessionId != null, SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);
        lqw.orderByDesc(SmsFlashPromotionProductRelation::getSort);
        lqw.orderByDesc(SmsFlashPromotionProductRelation::getId);
        relationMapper.selectPage(pageInfo,lqw);

        tableDataInfo.setList(pageInfo.getRecords());
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        tableDataInfo.setTotalPage(pageInfo.getPages());
        tableDataInfo.setTotal(pageInfo.getTotal());


       relationMapper.listAll(flashPromotionId,flashPromotionSessionId);

        return tableDataInfo;

    }*/


    /*@Override
    public AjaxResult listAll(Integer pageNum, Integer pageSize, Long flashPromotionId, Long flashPromotionSessionId) {
        //构造分页构造器
        Page<SmsFlashPromotionProductRelation> pageInfo = new Page<>(pageNum, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(flashPromotionId != null, SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        lqw.eq(flashPromotionSessionId != null, SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);
        lqw.orderByDesc(SmsFlashPromotionProductRelation::getSort);
        lqw.orderByDesc(SmsFlashPromotionProductRelation::getId);
        relationMapper.selectPage(pageInfo,lqw);
        List<SmsFlashPromotionProductRelation> records = pageInfo.getRecords();
        for (int i = 0; i < records.size(); i++) {
            SmsFlashPromotionProductRelation relation = records.get(i);
            Long productId = relation.getProductId();
            if (productId!=null){
                PmsProduct product = pmsProductService.getById(productId);
                relation.setProduct(product);
            }
        }

        TableDataInfo tableDataInfo = new TableDataInfo();

        tableDataInfo.setList(pageInfo.getRecords());
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        tableDataInfo.setTotalPage(pageInfo.getPages());
        tableDataInfo.setTotal(pageInfo.getTotal());

        return AjaxResult.success(tableDataInfo);

    }*/


    //分页查询相关商品及限时购促销信息
    @Override
    public TableDataInfo listAll(Integer pageNum, Integer pageSize, Long flashPromotionId, Long flashPromotionSessionId) {
        Page<SmsFlashPromotionProductRelation> pageInfo = new Page<>(pageNum,pageSize);
        TableDataInfo tableDataInfo = new TableDataInfo();
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(flashPromotionId != null, SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        lqw.eq(flashPromotionSessionId != null, SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);
        lqw.orderByDesc(SmsFlashPromotionProductRelation::getSort);
        lqw.orderByDesc(SmsFlashPromotionProductRelation::getId);
        relationMapper.selectPage(pageInfo,lqw);

            List<SmsFlashPromotionProductRelation> list = relationMapper.listAll(flashPromotionId, flashPromotionSessionId);
            for (SmsFlashPromotionProductRelation sms : list) {
                long productId = sms.getProductId();
                PmsProduct product = pmsProductService.getById(productId);
                sms.setProduct(product);
            }
        pageInfo.setRecords(list);

        tableDataInfo.setList(pageInfo.getRecords());
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        tableDataInfo.setTotalPage(pageInfo.getPages());
        tableDataInfo.setTotal(pageInfo.getTotal());





        return tableDataInfo;

    }
}
